#pragma once

#include<iostream>
#include<sys/epoll.h>
#include"Log.hpp"
#include"Common.hpp"
using namespace LogModule;

namespace EpollModule
{
    class epoll
    {
        public:
        epoll():_epfd(1)
        {}
        void Init()// 创建epoll句柄
        {
            _epfd=epoll_create(256);
            if(_epfd<0)
            {
                LOG(LogLevel::ERROR)<<"epoll_create error";
                return;
            }
            LOG(LogLevel::INFO)<<"epoll_create success,epfd "<<_epfd;
        }
        int wait(int num,struct epoll_event revs[],int timeout)// 等待事件发生
        {
            int n=epoll_wait(_epfd,revs,num,timeout);
            if(n<0)
            {
                LOG(LogLevel::ERROR)<<"epoll_wait error";
            }
            return n;
        }
        void Ctrl(int sockfd,uint32_t events)// 控制事件
        {
            struct epoll_event ev;
            ev.events=events;
            ev.data.fd=sockfd;
            int n=epoll_ctl(_epfd,EPOLL_CTL_ADD,sockfd,&ev);
            if(n<0)
            {
                LOG(LogLevel::ERROR)<<"epoll_ctl error";
            }

        }
        ~epoll()
        {

        }
    private:
        int _epfd;// epoll句柄
    };
}